package com.xiaotu.spider.xunyi.pageprocessor;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.StarBrandModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 XunYiPageProcessor
 * @日期 2017年11月22日
 * @作者 WangYanLong
 * @功能 寻艺明星代言品牌数据爬取
 */

@Component("XunYiPageProcessor")
public class XunYiPageProcessor extends AUrlPageProcessor{

	@Override
	protected String[] getStartRequestUrls() {
		return new String[] {"http://www.xunyee.cn/brandstar.html"};
	}
	
	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
        if (url.contains("http://www.xunyee.cn/brandstar")){
        	 return Constants.PageDataType.SEARCH_LIST;
        }else{
        	 return Constants.PageDataType.SEARCH_LIST;
        }
	}
	
	@Override
	public void process(Page page) {
		//明星品牌列表
		List<String> brandList =page.getHtml().xpath("//div[@class='brandstar_table']//table[@class='table table_scroll brandstar_table']/tbody/tr").all();
		if(brandList.isEmpty())
       	 throw new SpiderException(
	                    SpiderExceptionCode.DataInvalideException);
		
		String brandSmall=null;
		String[] brandName=null;
		String[] brandImg=null;
		String number = null;
		String starName =null;
		List<StarBrandModel> list = new ArrayList<StarBrandModel>();
		for (String brandSmallStr : brandList) {
			Html actorHtml = new Html(brandSmallStr);
			//明星编号
			number =actorHtml.xpath("//div[@class='row brandstar_tablecolumn_actor']//div[@class='col-xs-12 brandstar_tablecolumn_actor']//p[1]/strong/text()").toString();
			List<String> brandSmallList = actorHtml.xpath("//div[@class='row brandstar_tablecolumn_actor']//div[@class='col-xs-12 brandstar_tablecolumn_actor']"
					+ "//p/text()").all();
			//明星参与量
			brandSmall = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, brandSmallList.toString());
			//姓名
			String starNames =actorHtml.xpath("//div[@class='row brandstar_tablecolumn_actor']//div//p[@class='brandstar_p_small brandstar_p_rank']").get();
			starName = starNames.replaceAll("&nbsp;&nbsp; ", "");
			Html nameStr =new Html(starName);
			starName=nameStr.xpath("//p/text()").toString().trim();
			List<String> brandNameImg =actorHtml.xpath("//div[@class='row brandstar_tablerow_brand']//div[@class='col-xs-2 brandstar_tablecolumn_brand']").all(); 
			if(brandNameImg.size()==0){
				continue;
			}
			LinkedList<Object> brandNameList = new LinkedList<>();
			LinkedList<Object> brandImgList = new LinkedList<>();
			for (String brandStr : brandNameImg) {
				Html htmlStr = new Html(brandStr);
				String brandImgRes =htmlStr.xpath("//div[@class='col-xs-2 brandstar_tablecolumn_brand']//a/img/@src").get();
				brandImgList.add(brandImgRes);
				
				String brandNameRes= htmlStr.xpath("//div[@class='col-xs-2 brandstar_tablecolumn_brand']//a/img/@brand_title").get();
				brandNameList.add(brandNameRes);
			}
			//品牌名称
			brandName = brandNameList.toArray(new String[0]);
			//品牌logo
			brandImg = brandImgList.toArray(new String[0]);
		
			list.add(new StarBrandModel(Constants.TargetType.OTHER_TYPE_ID,starName,brandName, brandImg, brandSmall,number));
		}
			page.putField(StarBrandModel.class.getName(), list);
		
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.STAR_XUNYI;
	}

}
